import torch
import torch.nn as nn
from torch import optim
import torch.nn.functional as F
import config

class DecoderRNN(nn.Module):
    """
    traditional seq2seq decoder without attention
    """
    def __init__(self, hidden_size, output_size):
        super(DecoderRNN, self).__init__()
        self.hidden_size = hidden_size
        self.embedding = nn.Embedding(output_size, hidden_size)
        self.gru = nn.GRU(hidden_size, hidden_size)
        self.out = nn.Linear(hidden_size, output_size)
        self.softmax = nn.LogSoftmax(dim=1)

    def forward(self, inputs, hidden):
        output = self.embedding(inputs).view(1, 1, -1)
        output = F.relu(output)
        output, hidden = self.gru(output, hidden)
        output = self.softmax(self.out(output[0]))
        return output, hidden

    def initHidden(self):
        return torch.zeros(1, 1, self.hidden_size, device=config.device)


class EncoderRNN(nn.Module):
    def __init__(self, in_vocab_size, hidden_size):
        super(EncoderRNN, self).__init__()
        self.hidden_size = hidden_size
        self.embedding = nn.Embedding(in_vocab_size, hidden_size)
        self.gru = nn.GRU(hidden_size, hidden_size)

    def forward(self, inputs, hidden):
        embedded = self.embedding(inputs).view(1, 1, -1)
        # embedded layer need to be modified to accept bucketing
        output = embedded
        output, hidden = self.gru(output, hidden)
        return output, hidden

    def initHidden(self):
        return torch.zeros(1, 1, self.hidden_size, device=config.device)


class AttnDecoderRNN(nn.Module):
    def __init__(self,  output_vocab_size, hidden_size, dropout_p=0.1, max_length=config.max_len):
        """
        :param max_length: Manual set max output length limit
        """
        super(AttnDecoderRNN, self).__init__()
        self.hidden_size = hidden_size
        self.output_vocab_size = output_vocab_size
        self.dropout_p = dropout_p
        self.max_length = max_length
        self.embedding = nn.Embedding(self.output_vocab_size, self.hidden_size)
        self.attn = nn.Linear(self.hidden_size * 2, self.max_length)
        self.attn_combine = nn.Linear(self.hidden_size * 2, self.hidden_size)
        self.dropout = nn.Dropout(self.dropout_p)
        self.gru = nn.GRU(self.hidden_size, self.hidden_size)
        self.out = nn.Linear(self.hidden_size, self.output_vocab_size)

    def forward(self, inputs, hidden, encoder_outputs):
        embedded = self.embedding(inputs).view(1, 1, -1)
        # [len_seq, batch_size, d_model]
        embedded = self.dropout(embedded)
        # drop 几个单词维度,感觉不是很有必要。
        attn_weights = F.softmax(
            self.attn(torch.cat((embedded[0], hidden[0]), 1)), dim=1)
        # attn_weights: [batch_size, max_output_length]
        attn_applied = torch.bmm(attn_weights.unsqueeze(0),
                                 encoder_outputs.unsqueeze(0))
        # encoder outputs: after padding is [max_output_len, d_model]
        # attn_applied: [1,batch_size,d_model]
        output = torch.cat((embedded[0], attn_applied[0]), 1)
        # output: [batch_size,d_model*2]
        output = self.attn_combine(output).unsqueeze(0)
        # [1,batch_size,d_model]
        output = F.relu(output)
        output, hidden = self.gru(output, hidden)
        # [1,batch_size,d_model]
        output = F.log_softmax(self.out(output[0]), dim=1)
        return output, hidden, attn_weights

    def initHidden(self):
        return torch.zeros(1, 1, self.hidden_size, device=config.device)

